package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.entity.LeaseTerm;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.GraphInfoMapper;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.mapper.LeaseTermMapper;
import com.atguigu.lease.web.app.mapper.PaymentTypeMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

	@Autowired
	private LeaseAgreementMapper leaseAgreementMapper;
	@Autowired
	private ApartmentInfoService apartmentInfoService;
	@Autowired
	private RoomInfoService roomInfoService;
	@Autowired
	private LeaseTermMapper leaseTermMapper;
	@Autowired
	private PaymentTypeMapper paymentTypeMapper;
	@Autowired
	private GraphInfoMapper graphInfoMapper;


	@Override
	public List<AgreementItemVo> listItemByPhone(String phone) {
		return leaseAgreementMapper.listItemByPhone(phone);
	}

	@Override
	public AgreementDetailVo getDetailById(Long id) {
		// 查询租约信息
		LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
		// 查询公寓信息
		ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(leaseAgreement.getApartmentId());
		// 查询房间信息
		RoomDetailVo roomDetailVo = roomInfoService.getDetailById(leaseAgreement.getRoomId());
		// 查询租期信息
		LeaseTerm leaseTerm = leaseTermMapper.selectById(id);
		// 查询支付方式
		PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());
		// 查询公寓图片
		List<GraphVo> apartmentGraphVo = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, apartmentItemVo.getId());
		// 查询房间图片
		List<GraphVo> roomGraphVo = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, roomDetailVo.getId());

		AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
		BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
		// 租期单位
		agreementDetailVo.setLeaseTermUnit(leaseTerm.getUnit());
		// 租期月数
		agreementDetailVo.setLeaseTermMonthCount(leaseTerm.getMonthCount());
		// 支付方式
		agreementDetailVo.setPaymentTypeName(paymentType.getName());
		// 公寓图片
		agreementDetailVo.setApartmentGraphVoList(apartmentGraphVo);
		// 房间图片
		agreementDetailVo.setRoomGraphVoList(roomGraphVo);
		// 房间号
		agreementDetailVo.setRoomNumber(roomDetailVo.getRoomNumber());
		// 公寓名称
		agreementDetailVo.setApartmentName(apartmentItemVo.getName());
		return agreementDetailVo;
	}
}




